package com.twu.biblioteca.service;

import com.twu.biblioteca.IRender;
import com.twu.biblioteca.ui.IActuator;
import com.twu.biblioteca.ui.recevier.IntegerRecevier;
import com.twu.biblioteca.ui.recevier.StringRecevier;

import java.util.Scanner;

public class ActuatorBuilder {
    private final BookService bookService;
    private final MovieService movieService;
    private final Scanner scanner;
    private final IRender render;
    private final UserService userService;

    public ActuatorBuilder(final BookService bookService, final MovieService movieService, final UserService userService, Scanner scanner, IRender render) {
        this.bookService = bookService;
        this.movieService = movieService;
        this.userService = userService;
        this.scanner = scanner;
        this.render = render;
    }

    public IActuator buildQuitActuator() {
        return new IActuator() {
            @Override
            public void actuate() {
                System.exit(0);
            }
        };
    }

    public IActuator buildBookReturnActuator() {
        return new IActuator() {
            @Override
            public void actuate() {
                render.render(Message.INPUT_BOOK_ID);
                IntegerRecevier recevier = new IntegerRecevier(scanner);
                int bookId = recevier.getInteger();
                if (bookId == 0) {
                    render.render(Message.WRONG_INPUT);
                } else {
                    if (bookService.returnBook(bookId)) {
                        render.render(Message.THANK_RETURNING_BOOK);
                    } else {
                        render.render(Message.NOT_A_VALID_BOOK);
                    }
                }
                render.renderNewLine();
            }
        };
    }

    public IActuator buildBookCheckOutActuator() {
        return new IActuator() {
            @Override
            public void actuate() {
                render.render(Message.INPUT_BOOK_ID);
                IntegerRecevier recevier = new IntegerRecevier(scanner);
                int bookId = recevier.getInteger();
                if (bookId == 0) {
                    render.render(Message.WRONG_INPUT);
                } else {
                    if (bookService.checkout(bookId)) {
                        render.render(Message.ENJOY_THE_BOOK);
                    } else {
                        render.render(Message.BOOK_NOT_AVAILABLE);
                    }
                }
                render.renderNewLine();
            }
        };
    }

    public IActuator buildListBooksActuator() {
        return new IActuator() {
            @Override
            public void actuate() {
                render.render(bookService.listBooks());
                render.renderNewLine();
            }
        };
    }

    public IActuator buildListMoviesActuator() {
        return new IActuator() {
            @Override
            public void actuate() {
                render.render(movieService.listMovies());
                render.renderNewLine();
            }
        };
    }

    public IActuator buildMovieCheckOutActuator() {
        return new IActuator() {
            @Override
            public void actuate() {
                render.render(Message.INPUT_MOVIE_ID);
                IntegerRecevier recevier = new IntegerRecevier(scanner);
                int movieId = recevier.getInteger();
                if (movieId == 0) {
                    render.render(Message.WRONG_INPUT);
                } else {
                    if (movieService.checkout(movieId)) {
                        render.render(Message.ENJOY_THE_MOVIE);
                    } else {
                        render.render(Message.MOVIE_NOT_AVAILABLE);
                    }
                }
                render.renderNewLine();
            }
        };
    }

    public IActuator buildUserLoginActuator() {
        return new IActuator() {
            @Override
            public void actuate() {
                render.render("Input User Number:");
                StringRecevier numberRecevier = new StringRecevier(scanner);
                String number = numberRecevier.getString();
                render.render("Input password:");
                StringRecevier passwordRecevier = new StringRecevier(scanner);
                String password = passwordRecevier.getString();
                if (number.equals("") || password.equals("")) {
                    render.render(Message.WRONG_INPUT);
                } else {
                    if (userService.login(number, password)) {
                        render.render(Message.LOGIN_SUCCESS);
                    } else {
                        render.render(Message.LOGIN_FAIL);
                    }
                }
                render.renderNewLine();
            }
        };
    }

    public IActuator buildUserInformationActuator() {
        return new IActuator() {
            @Override
            public void actuate() {
                render.render(userService.showInformation());
                render.renderNewLine();
            }
        };
    }
}
